package com.dcy.znoa.controller.attendance;

import com.dcy.znoa.common.annotation.AuthenticationAPI;
import com.dcy.znoa.common.controller.BaseController;
import com.dcy.znoa.common.exception.BusinessException;
import com.dcy.znoa.common.util.pojo.JsonCommon;
import com.dcy.znoa.common.util.pojo.JsonForList;
import com.dcy.znoa.controller.attendance.model.attRule.AddAttRuleModel;
import com.dcy.znoa.controller.attendance.model.attRule.SelectAttRuleModel;
import com.dcy.znoa.controller.attendance.model.attRule.UpdateAttRuleModel;
import com.dcy.znoa.domain.attendance.AttRule;
import com.dcy.znoa.service.attendance.AttRuleService;
import com.wordnik.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.util.Date;

import static com.dcy.znoa.common.util.pojo.JsonCommon.SERVICE_ERROR_CODE;
import static com.dcy.znoa.common.util.pojo.JsonCommon.SUCCESS_CODE;

/**
 * 考勤规则表
 *
 * @author pengyu
 */
@Controller
@RequestMapping("attRule")
public class AttRuleController extends BaseController {
    @Autowired
    AttRuleService attRuleService;

    //WEB  查询规则列表，包括人数，是个复杂接口
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/query")
    public JsonForList query(SelectAttRuleModel model) {
        return attRuleService.queryListMapper(model);
    }

    //APP  查询单人的规则，以及地址列表
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/queryByOperId")
    public JsonCommon queryByOperId(Long operId) throws BusinessException, ParseException {
        if(operId==null){
            return new JsonCommon("404", "operId不能为空");
        }
        AttRule attRule = attRuleService.queryByOperId(operId);
        return new JsonCommon(attRule);
    }


    //WEB 按考勤规则查询单个数据
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/getById")
    @ApiOperation(value = "按ID查询考勤规则", httpMethod = "GET",
            consumes="application/json;charset=UFT-8",produces="application/json;charset=UFT-8",
            notes = "按ID查询考勤规则")
    public JsonCommon getById(Long attRuleId) throws BusinessException {
        if(attRuleId==null){
            return new JsonCommon("404", "attRuleId不能为空");
        }
        AttRule attRule = attRuleService.getById(attRuleId);
        return new JsonCommon(attRule);
    }

    //WEB
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/add")
    public JsonCommon add(@Valid AddAttRuleModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()) {
            return new JsonCommon("404", bindingResult.getFieldError().getDefaultMessage());
        }
        AttRule cp = new AttRule();
//        model.setCreateAttRuleId(getCurrentUser().getAttRuleId());
        model.setCreateManId(getCurrentUser().getOperId());
        model.setCreateTime(new Date());
        //时间格式的转换
//        String startTimeStr = model.getStartTimeStr();
//        String endTimeStr = model.getEndTimeStr();
//        Date startTime = DateTimeUtil.getHMSDate(startTimeStr);
//        Date endTime = DateTimeUtil.getHMSDate(endTimeStr);
//        model.setStartTime(startTime);
//        model.setEndTime(endTime);
        BeanUtils.copyProperties(cp, model);
            attRuleService.insert(cp);
        return new JsonCommon(SUCCESS_CODE);
    }

    //WEB
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/update")
    public JsonCommon update(@Valid UpdateAttRuleModel model, BindingResult bindingResult) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (bindingResult.hasErrors()) {
            return new JsonCommon("404", bindingResult.getFieldError().getDefaultMessage());
        }
        AttRule cp = new AttRule();
        model.setUpdateManId(getCurrentUser().getOperId());
        model.setUpdateTime(new Date());
        //时间格式的转换
//        String startTimeStr = model.getStartTimeStr();
//        String endTimeStr = model.getEndTimeStr();
//        Date startTime = DateTimeUtil.getHMSDate(startTimeStr);
//        Date endTime = DateTimeUtil.getHMSDate(endTimeStr);
//        model.setStartTime(startTime);
//        model.setEndTime(endTime);
        BeanUtils.copyProperties(cp, model);
        attRuleService.updateById(cp);
        return new JsonCommon(SUCCESS_CODE);
    }

    //WEB 禁用, 需要判断是否有用户关联
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/validFlag")
    public JsonCommon validFlag(Long attRuleId,Integer validFlag) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (attRuleId == null) {
            return new JsonCommon("404", "规则ID不能为空");
        }
        if(validFlag==null){
            validFlag = 0;
        }
        AttRule cp = attRuleService.getById(attRuleId);
        if(cp == null){
            return new JsonCommon("404", "系统内无此考勤ID");
        }
//        cp.setAttRuleId(attRuleId);
        cp.setUpdateManId(getCurrentUser().getOperId());
        cp.setUpdateTime(new Date());
        cp.setValidFlag(validFlag);
        attRuleService.updateById(cp);
        return new JsonCommon(SUCCESS_CODE);
    }

    //WEB 物理删除 需要判断是否有用户关联
    @AuthenticationAPI
    @ResponseBody
    @RequestMapping("/delete")
    public JsonCommon delete(Long attRuleId) throws BusinessException, InvocationTargetException, IllegalAccessException {
        if (attRuleId == null) {
            return new JsonCommon("404", "规则ID不能为空");
        }
        AttRule cp = new AttRule();
        cp.setUpdateManId(getCurrentUser().getOperId());
        cp.setUpdateTime(new Date());
//        cp.setValidFlag(0);
        if(attRuleService.delete(attRuleId)>0){
            return new JsonCommon(SUCCESS_CODE);
        }
        return new JsonCommon(SERVICE_ERROR_CODE);
    }

}
